我正在处理个人C#应用程序(类似钱包),我有一个查询显示所有表格中的所有费用(所有int列的总和)
SELECT l.Luna, l.An,
sa.Salariu, sa.Bonuri, sa.Bonus,
x.Afara, x.Vacanta, x.Cumparaturi_Diverse, x.PC,
z.Cumparaturi_Piata, z.FastFood, z.Mancare_Munca,
xa.Benzina, xa.Masina_Altele,
xz.Intretinere, xz.Internet_TV, xz.Telefon, xz.Datorii,
dif.Cont
From dbo.Luna l
INNER JOIN (SELECT Luna, AN, SUM(s.Salariu) AS Salariu, SUM(s.Bonuri) AS
Bonuri, SUM(s.Bonus) AS Bonus FROM dbo.Salariu s GROUP BY Luna, An) sa ON l.Luna = sa.Luna and l.An = sa.An
INNER JOIN (SELECT Luna, An, SUM(a.Afara) AS Afara, SUM(a.Vacanta) AS Vacanta, SUM(a.Cumparaturi_Diverse) AS Cumparaturi_Diverse, SUM(a.PC) AS PC FROM dbo.Altele a GROUP BY Luna, AN) x ON l.Luna = x.Luna and l.An = x.An
INNER JOIN (SELECT Luna, An, SUM(m.Cumparaturi_Piata) AS Cumparaturi_Piata, SUM(m.FastFood) AS FastFood, SUM(m.Munca) AS Mancare_Munca FROM dbo.Cumparaturi m GROUP BY Luna, AN) z ON l.Luna = z.Luna and l.An = z.An
INNER JOIN (SELECT Luna, An, SUM(ma.Benzina) AS Benzina, SUM(ma.Altele) AS Masina_Altele FROM dbo.Masina ma GROUP BY Luna, AN) XA ON l.Luna = xa.Luna and l.An = xa.An
INNER JOIN (SELECT Luna, An, SUM(u.Intretinere) AS Intretinere, SUM(u.Internet_TV) AS Internet_TV, SUM(u.Telefon) AS Telefon, SUM(u.Altele) AS Datorii, SUM(u.Chirie) AS Chirie FROM dbo.Utilitati u group by Luna, AN) xz ON l.Luna = xz.Luna and l.An = xz.An
INNER JOIN dbo.Diferenta dif ON l.Luna = dif.Luna and l.AN = dif.An
WHERE l.Luna in ('December', 'January', 'February') and l.An in (2017, 2018, 2019)
在我想要的地方也可以搜索只在我有INNER JOINS(列名= App_User)的表中可用的列。 所以我的问题是,如何将此查询添加到App_User列(即varchar)。 应该看起来像这样:
WHERE l.Luna in ('December', 'January', 'February') and l.An in (2017, 2018, 2019) and App_User = @App_User (a parameter that will take the value from c#)
答案 0 :(得分:0)
所以我找到了我的问题的解决方案,我必须在所有内部联接WHERE s.App_User = @App_User中添加一个where子句,并且我还添加了OR ISNULL(@App_User,'')='',如果参数如果是空白则应显示所有数据,因此查询现在如下所示:
declare @App_User varchar(255) = 'test'
SELECT DISTINCT
s.Luna, s.An,
sa.Salariu, sa.Bonuri, sa.Bonus,
x.Afara, x.Vacanta, x.Cumparaturi_Diverse, x.PC,
z.Cumparaturi_Piata, z.FastFood, z.Mancare_Munca,
xa.Benzina, xa.Masina_Altele,
xz.Intretinere, xz.Internet_TV, xz.Telefon,
lu.LunaID
From dbo.Salariu s
INNER JOIN (SELECT Luna, AN, SUM(s.Salariu) AS Salariu, SUM(s.Bonuri) AS Bonuri, SUM(s.Bonus) AS Bonus FROM dbo.Salariu s WHERE s.App_User = @App_User OR ISNULL(@App_User, '') = '' GROUP BY Luna, An) sa ON s.Luna = sa.Luna and s.An = sa.An
INNER JOIN (SELECT Luna, An, SUM(a.Afara) AS Afara, SUM(a.Vacanta) AS Vacanta, SUM(a.Cumparaturi_Diverse) AS Cumparaturi_Diverse, SUM(a.PC) AS PC FROM dbo.Altele a WHERE a.App_User = @App_User OR ISNULL(@App_User, '') = '' GROUP BY Luna, AN) x ON s.Luna = x.Luna and s.An = x.An
INNER JOIN (SELECT Luna, An, SUM(m.Cumparaturi_Piata) AS Cumparaturi_Piata, SUM(m.FastFood) AS FastFood, SUM(m.Munca) AS Mancare_Munca FROM dbo.Cumparaturi m WHERE m.App_User = @App_User OR ISNULL(@App_User, '') = '' GROUP BY Luna, AN) z ON s.Luna = z.Luna and s.An = z.An
INNER JOIN (SELECT Luna, An, SUM(ma.Benzina) AS Benzina, SUM(ma.Altele) AS Masina_Altele FROM dbo.Masina ma WHERE ma.App_User = @App_User OR ISNULL(@App_User, '') = '' GROUP BY Luna, AN) XA ON s.Luna = xa.Luna and s.An = xa.An
INNER JOIN (SELECT Luna, An, SUM(u.Intretinere) AS Intretinere, SUM(u.Internet_TV) AS Internet_TV, SUM(u.Telefon) AS Telefon, SUM(u.Altele) AS Altele, SUM(u.Chirie) AS Chirie FROM dbo.Utilitati u WHERE u.App_User = @App_User OR ISNULL(@App_User, '') = '' GROUP BY Luna, AN) xz ON s.Luna = xz.Luna and s.An = xz.An
--INNER JOIN dbo.Diferenta dif ON s.Luna = dif.Luna and s.AN = dif.An
INNER JOIN dbo.Luna lu on s.Luna = lu.LUNA
WHERE s.Luna in ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'Octomber', 'December') and s.An in (2017, 2018, 2019)
ORDER BY s.An, lu.lunaID ASC