How can I join these queries?
SELECT RCTDT, SUM(RCTAMOUNT), COUNT(RCTAMOUNT) FROM RECEIPTS4
WHERE RCTDT BETWEEN '01-nov-2009' AND '30-nov-2009'
AND RCTTYPE='CA' AND RCTAMOUNT>0
GROUP BY RCTDT
---
SELECT RCTDT, SUM(RCTAMOUNT), COUNT(RCTAMOUNT) FROM RECEIPTS4
WHERE RCTDT BETWEEN '01-nov-2009' AND '30-nov-2009'
AND RCTTYPE='CQ' AND RCTAMOUNT>0
GROUP BY RCTDT
My table looks like this with duplicates in col1
col1, col2, col3, col4
1, 1, 0, a
1, 2, 1, a
1, 3, 1, a
2, 4, 1, b
3, 5, 0, c
I want to select distinct col1 with max (col3) and min(col2);
so result set will be:
col1, col2, col3, col4
1, 2, 1, a
2, 4, 1, b
3, 5, 0, c
I have a solution but looking for best ideas?
Hi
I am using Linq2Sql and want to bind an objects field (which is enum) to either a bit or a int type in the database. For example I want have a gender field in my model. I have already edited the DBML and changed the Type to point to my enum. I want to create Radio buttons (which I think I have figured out) for gender and dropdown lists for other areas using the same idea. My enum looks like this
public enum Gender
{
Male,
Female
}
Mapping between DbType 'int' and Type 'Project.Models.Gender' in Column 'Gender' of Type 'Candidate' is not supported.
Any ideas on how to do this mapping. Am I missing something on the enums.
i have one independent form where i am checking read only access of an xml file ,
if the file is read only then i have to display message in the status bar of MDI form.
since i am using independent form to valid xml file, status bar of the MDI form is not displaying the error message.
now how to display message?
thanx in advance
i am converting from access to mysql
i have a table in access where one of the columns is an autonumber
when i transfer the data into the mysql database (where i also have a column that is auto_increment), should i be transfering the auto_increment data into the auto_increment column, or will it auto_increment itself?
how do i ensure that if i do not transfer the autoincrement data from access, that it auto_increments properly?
iam having 2 tables
table Items Table (this table holds all items iam having)
itemId
---------
Item1
Item2
Item3
Item4
Item5
table 2 users_item relation
UserId || ItemId
1 || Item1
1 || Item2
userId one has stored 2 items Item1,Item2.
Now i want to write a query on table1 (Items table) so that it displays all items which user1 has NOT chosen.
Now we have a firebird database with 1.000.000 that must be processed after ALL are loaded in RAM memory. To get all of those we must extract data using (select * first 1000 ...) for 8 hours. What is the solution for this?
i have a database that already has a users table
COLUMNS:
userID - int
loginName - string
First - string
Last - string
i just installed the asp.net membership table. Right now all of my tables are joined into my users table foreign keyed into the "userId" field
How do i integrate asp.net_users table into my schema? here are the ideas i thought of:
Add a membership_id field to my users table and on new inserts, include that new field in my users table. This seems like the cleanest way as i dont need to break any existing relationships.
break all existing relationship and move all of the fields in my user table into the asp.net_users table. This seems like a pain but ultimately will lead to the most simple, normalized solution
any thoughts?
I've written some really nice, funky libraries for use in LinqToSql. (Some day when I have time to think about it I might make it open source... :) )
Anyway, I'm not sure if this is related to my libraries or not, but I've discovered that when I have a large number of changed objects in one transaction, and then call DataContext.GetChangeSet(), things start getting reaalllly slooowwwww. When I break into the code, I find that my program is spinning its wheels doing an awful lot of Equals() comparisons between the objects in the change set. I can't guarantee this is true, but I suspect that if there are n objects in the change set, then the call to GetChangeSet() is causing every object to be compared to every other object for equivalence, i.e. at best (n^2-n)/2 calls to Equals()...
Yes, of course I could commit each object separately, but that kinda defeats the purpose of transactions. And in the program I'm writing, I could have a batch job containing 100,000 separate items, that all need to be committed together. Around 5 billion comparisons there.
So the question is: (1) is my assessment of the situation correct? Do you get this behavior in pure, textbook LinqToSql, or is this something my libraries are doing? And (2) is there a standard/reasonable workaround so that I can create my batch without making the program geometrically slower with every extra object in the change set?
I have a mysql table "items" with 2 integer fields: seid and tiid
The table has about 35000000 records, so it's very large.
seid tiid
-----------
1 1
2 2
2 3
2 4
3 4
4 1
4 2
The table has a primary key on both fields, an index on seid and an index on tiid.
Someone types in 1 or more tiid values and now I would like to get the seid with most results.
For example when someone types 1,2,3, I would like to get seid 2 and 4 as result. They both have 2 matches on the tiid values.
My query so far:
SELECT COUNT(*) as c, seid
FROM items
WHERE tiid IN (1,2,3)
GROUP BY seid
HAVING c = (SELECT COUNT(*) as c, seid
FROM items
WHERE tiid IN (1,2,3)
GROUP BY seid
ORDER BY c DESC
LIMIT 1)
But this query is extremly slow, because of the large table.
Does anyone know how to construct a better query for this purpose?
i have a problem with my PHP-MSSQL query.
i have a join table that need to give a result something be like this:
Department Group A Group B Total A+B
WORKHOUR A OTHOUR A WORKHOUR B OTHOUR B WORKHOUR OTHOUR
HR 10 15 25 0 35 15
IT 5 5 5 5
Admin 12 12 12 12
the query will count how many employee as per given date (admin will enter data and once submitted, the query will give the above result).
The problem is, the final output is a mess when there's no row to be displayed. the column is shifted to the right.
i.e:
only Group A in IT
only Group B in Admin
Department Group A Group B Total A+B
WORKHOUR A OTHOUR A WORKHOUR B OTHOUR B WORKHOUR OTHOUR
HR 10 15 25 0 35 15
IT 5 5 5 5
Admin 12 12 12 12
my question is, how to prevent this to happen?
i've tried everything with While.... if else.. but the result is still the same.
how to display output "0" if no rows to return?
echo "0";
this is my QUERY:
select DD.DPT_ID,DPT.DEPARTMENT_NAME,TU.EMP_GROUP, sum(DD.WORK_HOUR) AS WORK_HOUR,
sum(DD.OT_HOUR) AS OT_HOUR
FROM DEPARTMENT_DETAIL DD
left join DEPARTMENT DPT
ON (DD.DEPT_ID=DPT.DEPT_ID)
LEFT JOIN TBL_USERS TU
ON (TU.EMP_ID=DD.EMP_ID)
WHERE DD_DATE>='2012-01-01'
AND DD_DATE<='2012-01-31'
AND TU.EMP_GROUP!=2
GROUP BY DD.DEPT_ID, DPT.DEPARTMENT_NAME,TU.EMP_GROUP
ORDER BY DPT.DEPARTMENT_NAME
this is one of the logic that i've used, but doesn't return the result that i want::
while($row = mssql_fetch_array($displayResult))
{
if ((!$row["WORK_HOUR"])&&(!$row["OT_HOUR"]))
{
echo "<td >";
echo "empty";
echo " </td>";
echo "<td >";
echo "empty";
echo " </td>";
}
else
{
echo "<td>";
echo $row["WORK_HOUR"];
echo " </td>";
echo "<td>";
echo $row["OT_HOUR"];
echo " </td>";
}
}
please help. i've been doing this for 2 days. @__@
i have a data-gride-view and i add my query to this when write my query i catch this error:
The schema returned by the new query differs from the base query
and this my query:
SELECT B.SettingKey, 'SysSettingsDep' AS TableName, B.SettingValue, B.SettingDesc
FROM SysCustomer AS A INNER JOIN
SysSettingsDep AS B ON A.SettingKey = B.SettingKey
UNION
SELECT C.SettingKey, 'SysSettingsMachine' AS TableName, C.SettingValue, C.SettingDesc
FROM SysCustomer AS A INNER JOIN
SysSettingsMachine AS C ON A.SettingKey = C.SettingKey
UNION
SELECT D.SettingKey, 'SysSettings' AS TableName, D.SettingValue, D.SettingDesc
FROM
SysCustomer AS A INNER JOIN SysSettings AS D ON A.SettingKey = D.SettingKey
help me to solve this,
tnx
I have a scenario which I'm a bit stuck on. Let's say I have a survey about colors, and I have one table for the color data, and another for people's answers.
tbColors
color_code , color_name
1 , 'blue'
2 , 'green'
3 , 'yellow'
4 , 'red'
tbAnswers
answer_id , favorite_color , least_favorite_color , color_im_allergic_to
1 , 1 , 2 3
2 , 3 , 1 4
3 , 1 , 1 2
4 , 2 , 3 4
For display I want to write a SELECT that presents the answers table but using the color_name column from tbColors.
I understand the "most stupid" way to do it naming tbColors three times in the FROM section, using a different alias for each column to replace.
How would a non-stupid way look?
I'm importing a flat file of invoices into a database using C#. I'm using the TransactionScope to roll back the entire operation if a problem is encountered.
It is a tricky input file, in that one row does not necessary equal one record. It also includes linked records. An invoice would have a header line, line items, and then a total line. Some of the invoices will need to be skipped, but I may not know it needs to be skipped until I reach the total line.
One strategy is to store the header, line items, and total line in memory, and save everything once the total line is reached. I'm pursuing that now.
However, I was wondering if it could be done a different way. Creating a "nested" transaction around the invoice, inserting the header row, and line items, then updating the invoice when the total line is reached. This "nested" transaction would roll back if it is determined the invoice needs to be skipped, but the overall transaction would continue.
Is this possible, practical, and how would you set this up?
Hi,
Fairly simple question, but I don't see it anywhere else on SO:
Do indexes (indices?) on a temporary table get automatically deleted with the temporary table?
I'd imagine they do but I don't really know how to check to make sure.
Thanks,
Phil
Using MySql 5, I have a task where I need to update one table based on the contents of another table.
For example, I need to add 'A1' to table 'A' if table 'B' contains 'B1'. I need to add 'A2a' and 'A2b' to table 'A' if table 'B' contains 'B2', etc.. In our case, the value in table 'B' we're interested is an enum.
Right now I have a stored procedure containing a series of statements like:
INSERT INTO A
SELECT 'A1'
FROM B
WHERE B.Value = 'B1';
--Repeat for 'B2' -> 'A2a'; 'B2' -> 'A2b'; 'B3' -> 'A3', etc...
Is there a nicer more DRY way of accomplishing this?
Edit:
There may be values in table 'B' that have no equivalent value for table 'A'.
Hi,
Does order of the columns in an Index definition for a table in a database has any effect on the performance?
for e.g. are these two queries different ?
CREATE INDEX xxx ON tablex(col1,col2)
CREATE INDEX xxx ON tablex(col2,col1)
what about the in case that I use a BTREE index?
I am using Mysql.
thanks
I'm working on a small project that involves grabbing a list of contacts which are stored for each group. Essentially, the database is set up so that each group has a primary and secondary contact stored as, unsurprisingly, Group.Primary and Group.Secondary. The objective is to pull every Primary and Secondary contact for each Group and display them in a sortable table.
I have the sortable table all worked out, but I have come across a small problem. Each primary and secondary field can have more than one contact separated by a comma. For instance, if Primary contained 123,256 , it would need to pull both Contacts with IDs 123 and 256. I had intended to use a query formatted like this:
SELECT *
FROM Group G,
Contacts C
WHERE G.Primary LIKE %C.ID%
OR G.Secondary LIKE %C.ID%
so that I could just skip the comma part, but I can't seem to find a working query for this.
My question to you is, am I just overlooking something here? Is there a simple query that would let me do this? Or am I better off getting the groups and contacts separately, and combine the two later. I think the former is a little easier to understand when read, which is a plus as this is a shared project, but if that is not possible I will do the latter.
This code is simplified, but it gets the point across.
I would like to make notice of some wierd thing. I designed an entire package application using VB.Net in Visual Studio 2008. I changed the normal backgrounds to Black, to make a diffrerence. but the problem is i1mm getting changing back ground colors when i install it on other systems.which does have all supports required.
What should i do to supress this problem.
i have one table bbc(country,region,area,population,gdp)
and i want to list the regions with total population of at least 100 million. be careful it is very tuff query
We are about to embark on a major project which requires an Occasionally Connected Application.
The MS Sync Framework seems to offer a good solution with Sync Services for ADO.NET.
Has anyone used this in a production app, ideally with tens of thousands of users, and can you comment on how well it scales? Any other pitfalls or gotcha's?
Thanks!
If I run Profiler, then it suggests a lot of indexes like this one
CREATE CLUSTERED INDEX [_dta_index_Users_c_9_292912115__K1] ON [dbo].[Users]
(
[UserId] ASC
)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF,
ONLINE = OFF) ON [PRIMARY]
UserId is the primary key of the table Users. Is this index better than the one already in the table:
ALTER TABLE [dbo].[Users] ADD CONSTRAINT [PK_Users] PRIMARY KEY NONCLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]