i am working on a c# project in that i have made this query which is
SELECT COUNT(customer_column)
FROM table2
WHERE Datepart(mm, record_column) = #myMonthSelected
AND Datepart(yy, record_column) = #myYearSelected
it counts the total number of rows after sorting and now i want it to modify that a column which is having a "Bit" data type, i want to count the total number of true rows by sorting month and year same as the above query. can it be possible to narrow my search?
Try this
SELECT COUNT(customer_column)
FROM table2
WHERE Datepart(mm, record_column) = #myMonthSelected
AND Datepart(yy, record_column) = #myYearSelected
AND BitColumn = 1
I use the old "sum of bits" trick. Like this:
SELECT SUM(someBitColumn) AS TotalCount
FROM someTable
Since I'm at it, here's a variation for counting types of things:
SELECT
SUM(
CASE WHEN animalKind = 'Dog' THEN 1
ELSE 0
END CASE
) AS DogCount,
SUM(
CASE WHEN animalKind = 'Cat' THEN 1
ELSE 0
END CASE
) AS CatCount,
...
FROM animals
Related
I have a Clients table already populated by thousands of records and now I need to search for a non-existing number in the card number column starting from the number x.
Example: I would like to search for the first available card number starting from number 2000.
Unfortunately I cannot select MAX() as there are records with 9999999 (which is the limit).
Is it possible to do this search through a single SELECT?
It's possible with a few nested SELECTs:
SELECT MIN(`card_number`) + 1 as next_available_number
FROM( SELECT (2000-1) as `card_number`
UNION
SELECT `card_number`
FROM clients
WHERE `card_number` >= 2000
) tmp
WHERE NOT EXISTS ( SELECT NULL
FROM clients
WHERE `card_number` = tmp.`card_number` + 1 )
It can be done with a self-join on your clients table, where you search for the lowest cardnumber for which the cardnumber + 1 does not exist.
In case x is 12, the query would be:
SELECT MIN(cardnumber) + 1
FROM clients
WHERE cardnumber + 1 NOT IN (SELECT cardnumber FROM clients)
AND cardnumber + 1 > 12
Eg. with a dataset of
INSERT INTO clients (cardnumber) VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(11),(12),(13),(14),(15),(17),(18)
this returns 16, but not 10.
Example on SQL Fiddle.
I think this is very similar to this question, but the minimum criteria is new.
If the credit card is represented as integer in your table and your starting number is 2000 you could do something like:
SELECT top 1 (card_id + 1)
FROM CreditCards t
WHERE card_id IN (
SELECT card_id
FROM CreditCards
WHERE card_id LIKE '%[2][0][0][0]%'
)
AND NOT EXISTS (SELECT 1 FROM CreditCards t2 WHERE t2.card_id = t.card_id + 1)
ORDER BY card_id
Example data (Table: CreditCards):
card_id
2000002
2000103
2000000
2000108
2000106
3000201
1000101
Result is: 2000001
Note that %[2][0][0][0]% is fixed here. You could also introduce a parameter.
It is not an optimal solution, but it does the trick.
I have a sperate table for each person where I store information about its presence and absence.
I want to calculate the value of 'present' and 'absent' in a column 'attendance'
table name is "teacher name".
Database looked like this.
Time Attendance
'2:30:30' 'Present'
'1:20:30' 'Present'
'4:10:30' 'Absent'[view of database in xampp][1]
'3:30:30' 'Present'
'2:32:30' 'Absent'
I want to calculate the number of occurrence of 'present' and 'absent'
so I can generate the salary according to it.
Is it possible?
Or I am doing something wrong?
SELECT Attendence, COUNT(*) FROM your_table
GROUP BY Attendence
Using LINQ, along the lines of:
.GroupBy (k => k.Attendence, (k, m) => new { Attendence = k, Count = m.Count() }
Per comment: If you need results horizontally rather than vertically, technically the proper way is to use PIVOT, but a quick and easy way is:
SELECT
SUM(CASE Attendence WHEN 'Present' THEN 1 ELSE 0 END) AS Present,
SUM(CASE Attendence WHEN 'Absent' THEN 1 ELSE 0 END) AS Absent
FROM your_table
I want to get alternate series of records using SQL Server.
For example :
I want to skip first 10 records (1 to 10) in sequence and get other 10 records (11 to 20) after that I want to skip next 10 records (21 to 30) and get another next 10 records (31 to 40)
I have done for alternate rows as below...
SELECT ROW, EmployeeID
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY EmployeeID) AS ROW, *
FROM Employee) A
WHERE
ROW % 2 = 0
But in case of my requirement above logic will not work. Please help me to make above thing works..
Linq will also accepted
Thanks
SELECT ROW, EmployeeID
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY EmployeeID) AS ROW, *
FROM Employee) A
WHERE
((ROW - 1)/10) % 2 = 1
I have done above of your requirement like below (Example).
Generated 1200 number sequentially from 1 to 1200 like below
;WITH CTE
AS
(
SELECT 1 PERIOD_SID
UNION ALL
SELECT PERIOD_SID+1 FROM CTE WHERE PERIOD_SID<1200
)
SELECT * INTO #PERIOD1 FROM CTE
OPTION(MAXRECURSION 0)
After this i have find alternative numbers like you have mentioned in your query
SELECT PERIOD_SID FROM
(
SELECT CASE
WHEN PERIOD_SID%10 <> 0 THEN PERIOD_SID / 10
WHEN PERIOD_SID%10 = 0 THEN ( PERIOD_SID / 10 ) - 1
END RNO,
PERIOD_SID
FROM #PERIOD1 )A
WHERE RNO%2=0
so if you have sequential numbers in your query (If you dont have generate using rownumber) then apply above logic.
Your query need to convert like below.
SELECT EMPLOYEEID
FROM (SELECT Row_number()
OVER (
ORDER BY EMPLOYEEID)AS ROW,
*
FROM EMPLOYEE) A
WHERE ( CASE
WHEN RNO%10 <> 0 THEN RNO / 10
WHEN RNO%10 = 0 THEN ( RNO / 10 ) - 1
END )%2 = 0
may be you can try this
SELECT ROW, EmployeeID FROM(
SELECT ROW_NUMBER()OVER (ORDER BY EmployeeID)AS ROW,* FROM Employee)
A WHERE ((ROW - (ROW%10))/10) % 2 = 1
Don't if this works or not coz I dont have sql server to run. Please show the error/result after running this query.
Ask if any doubt.
I know this question has already been asked many times.
The problem is that other solutions don't work..
I tried the following:
SELECT ID FROM TABLE_1 AS T1 WHERE NOT EXISTS (SELECT COLUMN_1,COLUMN_2 FROM TABLE_2 AS T2 WHERE T1.COLUMN_1 = T2.COLUMNS_1 AND T1.COLUMN_2 = T2.COLUMN_2);
It always go in timeout, both from Workbench and from code (I am using Visual Studio 2013 C#).
I don't know how to make the query easier in order to make it work.. Maybe split it in 2..
Example:
Table 1 Table 2
ID COLUMN_1 COLUMN_2 ID COLUMN_1 COLUMN_2
1 0 1 1 0 1
2 0 1 2 0 1
3 0 1 3 0 1
4 1 2
5 1 2
6 1 2
It should return
1 2
Or also only the ID (2).
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
LEFT JOIN Table_2
ON Table_1.ID = Table_2.ID
AND Table_1.COLUMN_1 = Table_2.COLUMN_1
AND Table_1.COLUMN_2 = Table_2.COLUMN_2
WHERE Table_2.ID IS NULL
Edit:
Well, if you don't need to match the id, then it's simply:
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
LEFT JOIN Table_2
ON Table_2.COLUMN_1 = Table_1.COLUMN_1
AND Table_2.COLUMN_2 = Table_1.COLUMN_2
WHERE Table_2.ID IS NULL
If that's still too slow, maybe an index can help.
If an index doesn't help, you can still increase the command timeout.
Still, another option would be:
SELECT
Table_1.ID
,Table_1.COLUMN_1
,Table_1.COLUMN_2
FROM Table_1
WHERE
(COLUMN_1, COLUMN_2) NOT IN (SELECT COLUMN_1, COLUMN_2 FROM Table_2)
I'm a MySQL guy these days and have a .NET project that I'm working on and can't figure out how to dynamically generate and return a field(column) from a table, based on two fields in the table.
There are two fields bRentable and bRented in the Units table and I need to return a field called reserved if bRentable and bRented are equal to zero.
Here is the SQL I have so far
/* Units Query */
SELECT Units.UnitID, Units.dcWidth, Units.dcLength, Units.dcPushRate,
Units.dcStdRate, Units.UnitTypeID, UnitTypes.sTypeName, Units.bRentable
FROM Units
INNER JOIN UnitTypes ON Units.UnitTypeID = UnitTypes.UnitTypeID
Any help would be greatly appreciated.
You can add the following as another column to your select statement:
(cast case
when (bRentable = 0 and bRented = 0) then 1
else 0
end as bit) as reserved
Edit: Updated based on OPs comment:
SELECT Units.UnitID, Units.dcWidth, Units.dcLength, Units.dcPushRate,
Units.dcStdRate, Units.UnitTypeID, UnitTypes.sTypeName, Units.bRentable,(cast case
when (bRentable = 0 and bRented = 0) then 1
else 0
end as bit) as reserved
FROM Units
INNER JOIN UnitTypes ON Units.UnitTypeID = UnitTypes.UnitTypeID
SELECT Units.UnitID, Units.dcWidth, Units.dcLength, Units.dcPushRate,
Units.dcStdRate, Units.UnitTypeID, UnitTypes.sTypeName, Units.bRentable,
CASE When (Units.bRentable = 0 and Units.bRented = 0) then 1 else 0 end as reserved
FROM Units
INNER JOIN UnitTypes ON Units.UnitTypeID = UnitTypes.UnitTypeID
After this query in your function or sproc,you can just check the values of the field reserved.If its 1 return.
if you'd like a stored proc or user defined function, the below query will help.
DECLARE #Rentable BIT
DECLARE #Rented BIT
DECLARE #Reserved BIT
SELECT #Rentable = U.bRentable, #Rented = U.bRented
FROM UNITS U INNER JOIN UnitTypes UT ON U.UnitTypeId = UT.UnitTypeId
IF(#Rentable = 0 AND #Rented = 0)
BEGIN
SET #Reserved = 0
END
ELSE
BEGIN
SET #Reserved = 1
END
RETURN #Reserved // OR SELECT #Reserved
Search for ADO.NET & using SqlCommand with parameters, you can pass SqlParameters after you frame the command with your query.
SqlCommand.Parameters.Add();